package com.zimi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.model.SysUser;
import com.zimi.model.WfRole;
import com.zimi.model.WfRoleUser;
import com.zimi.util.UserUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 流程角色表 api
 *
 * @author lnintr
 * @since 2019-07-11
 */
@RestController
@RequestMapping("wf-role")
public class WfRoleApi {

    @PostMapping("addWfRole")
    @ApiOperation("新增流程角色")
    @ResponseBody
    public Object addWfRole(WfRole record) {
        Map map = new HashMap();

        List<WfRole> bList = record.selectList(new QueryWrapper<WfRole>().lambda().eq(WfRole::getEnName, record.getEnName()).and(obj -> obj.eq(WfRole::getOrgId, record.getOrgId())));

        if (bList.size() > 0) {
            map.put("OK", "error");
            map.put("code", 90001); //同机构英文名重复
        } else {
            SysUser createUser = UserUtil.getUser();
            record.setCreateUser(createUser.getId());
            record.setCreateTime(new Date());
            record.insert();

            map.put("OK", "OK");
            map.put("code", 200);
        }

        return map;
    }

    @PostMapping("updateWfRole")
    @ApiOperation("更新流程角色")
    @ResponseBody
    public Object updateWfRole(WfRole record) {
        Map map = new HashMap();

        SysUser createUser = UserUtil.getUser();
        record.setUpdateUser(createUser.getId());
        record.setUpdateTime(new Date());
        record.updateById();

        map.put("OK", "OK");
        map.put("code", 200);

        return map;
    }

    @GetMapping("selectList")
    @ApiOperation("查询所有")
    @ResponseBody
    public Object selectList(WfRole record) {
        Map map = new HashMap();
        List<WfRole> listRole = new ArrayList<>();

        if (record.getOrgId() != null) {
            listRole = record.selectList(new QueryWrapper<WfRole>().lambda().setEntity(record));
        }

        map.put("List", listRole);
        map.put("OK", "OK");
        map.put("code", 200);

        return map;
    }

    @PostMapping("delWfRole")
    @ApiOperation("删除流程角色")
    @ResponseBody
    public Object delWfRole(WfRole record) {
        Map map = new HashMap();

        List<WfRoleUser> listRoleUser = new WfRoleUser().selectList(new QueryWrapper<WfRoleUser>().lambda().eq(WfRoleUser::getWfRoleId, record.getId()));
        if (listRoleUser.size() > 0) {

            map.put("OK", "error");
            map.put("code", 90001);
        } else {
            if (record.getId() != null && record.getId() > 0) {
                record.deleteById();
            }

            map.put("OK", "OK");
            map.put("code", 200);
        }

        return map;
    }
}
